package top.dotgo.web.base;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import top.dotgo.kit.Const;
import top.dotgo.kit.ContextKit;
import top.dotgo.kit.bean.AccInfoBean;
import top.dotgo.kit.ex.DotException;
import top.dotgo.kit.ex.DotMsg;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author jornl
 * @date 2020/3/2 18:17 星期一
 */
public class BaseController {
    public static final String UNKNOWN = "unknown";

    /**
     * 获取 HttpServletResponse
     *
     * @return HttpServletResponse
     */
    public static HttpServletResponse getResponse() {
        return (HttpServletResponse) ContextKit.getResponse();
    }

    /**
     * 获取 HttpServletRequest
     *
     * @return HttpServletRequest
     */
    public static HttpServletRequest getRequest() {
        return (HttpServletRequest) ContextKit.getRequest();
    }


    /**
     * 获取用户真是的ip地址，可使用getRemoteAddr()。如果用户使用了代理，使用getRemoteAddr获取不到真实的ip,
     *
     * @return String
     */
    public static String getRemoteIp() {
        HttpServletRequest request = getRequest();
        if (request == null) {
            return "";
        }
        String ip = request.getHeader("x-forwarded-for");
        if (ip != null && ip.length() != 0 && !UNKNOWN.equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个ip值，第一个ip才是真实ip
            if (ip.contains(StrUtil.COMMA)) {
                ip = ip.split(StrUtil.COMMA)[0];
            }
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 获取 token
     *
     * @return R
     */
    public String getToken() {
        return ContextKit.getToken();
    }

    /**
     * 获取账号id
     *
     * @return AccId
     */
    public Long getAccId() {
        Object accId = ContextKit.getAccId();
        if (!(accId instanceof Long)) {
            throw new DotException(DotMsg.LOGIN_ERR_MSG, DotMsg.LOGIN_ERR);
        }
        return (Long) accId;
    }

    /**
     * 获取账号类型
     *
     * @return Const.Type.acc
     */
    public Const.Type.acc getAccType() {
        return Const.Type.acc.load(getInfo().getAccType());
    }

    /**
     * 获取 getUserId
     *
     * @return AccId
     */
    public Long getUserId() {
        return getInfo().getUserId();
    }

    /**
     * 获取 TopCompanyId
     *
     * @return TopCompanyId
     */
    public Long getTopCompanyId() {
        return getInfo().getTopCompanyId();
    }

    /**
     * 获取 CompanyId
     *
     * @return CompanyId
     */
    public Long getCompanyId() {
        return getInfo().getCompanyId();
    }

    /**
     * 获取 DepId
     *
     * @return DepId
     */
    public Long getDepId() {
        return getInfo().getDepId();
    }

    /**
     * 获取 AccInfoBean
     *
     * @return AccInfoBean
     */
    public AccInfoBean getInfo() {
        AccInfoBean bean = ContextKit.getAccInfo();
        if (bean == null) {
            throw new DotException(DotMsg.LOGIN_ERR_MSG, DotMsg.LOGIN_ERR);
        }
        return bean;
    }

    /**
     * 根据路径 和文件后缀（或原文件名）获取oss文件key
     *
     * @param path   path
     * @param suffix suffix
     * @return String
     */
    public String getFileKey(String path, String suffix) {
        //获取文件后缀
        if (!StrUtil.isEmpty(suffix)) {
            int index = suffix.lastIndexOf(StrUtil.DOT);
            if (index > 0) {
                suffix = suffix.substring(index);
            }
        }
        if (!suffix.startsWith(StrUtil.DOT)) {
            suffix = ".png";
        }

        return StrUtil.format("{}/{}{}{}", StrUtil.removePrefix(path, "/"), DateUtil.date().toString(DatePattern.PURE_DATETIME_PATTERN), StrUtil.uuid(), suffix);
    }
}
